package com.jr.controller;

import com.jr.mapper.ServiceTypeMapper;
import com.jr.pojo.ServiceType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/service-types")
@CrossOrigin(origins = "*")
public class ServiceTypeController {

    @Autowired
    private ServiceTypeMapper serviceTypeMapper;

    @GetMapping("/all")
    @ResponseBody
    public Map<String, Object> getAllServiceTypes() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<ServiceType> serviceTypes = serviceTypeMapper.selectAll();
            result.put("success", true);
            result.put("serviceTypes", serviceTypes);
            result.put("count", serviceTypes.size());
            result.put("message", "成功获取所有服务类型");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询服务类型失败: " + e.getMessage());
            System.err.println("查询服务类型失败: " + e.getMessage());
        }
        return result;
    }

    @GetMapping("/active")
    @ResponseBody
    public Map<String, Object> getActiveServiceTypes() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<ServiceType> serviceTypes = serviceTypeMapper.selectActive();
            result.put("success", true);
            result.put("serviceTypes", serviceTypes);
            result.put("count", serviceTypes.size());
            result.put("message", "成功获取活跃服务类型");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询活跃服务类型失败: " + e.getMessage());
            System.err.println("查询活跃服务类型失败: " + e.getMessage());
        }
        return result;
    }
}
